package com.dkm.ec.sale.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.persistence.entity.EavAttributePo;
import com.dkm.biz.gl.repository.EavAttributeRepository;
import com.dkm.ec.sale.persistence.dao.SalePriceDao;
import com.dkm.ec.sale.persistence.entity.SaleEntityPo;
import com.dkm.ec.sale.persistence.entity.SalePricePo;
import com.dkm.ec.sale.persistence.entity.SaleSkuPo;
import com.dkm.ec.sale.repository.SaleEntityRepository;
import com.dkm.ec.sale.repository.SalePriceRepository;
import com.dkm.ec.sale.repository.SaleSkuRepository;
import com.dkm.ec.sale.service.SaleSkuService;

/**
 * 对象功能:产品销售价格 领域对象实体 开发公司:SKG.COM 开发人员:Yuki 创建时间:2015-01-23 15:26:47
 */
@Service
@Scope("prototype")
public class SalePrice extends AbstractDomain<String, SalePricePo> {

	private SalePriceDao salePriceDao = null;

	@Resource
	private SalePriceRepository salePriceRepository;
	@Resource
	private EavAttributeRepository eavAttributeRepository;
	@Resource
	private SaleSkuRepository saleSkuRepository;
	@Resource
	private SaleSkuService saleSkuService;
	@Resource
	private SaleEntityRepository saleEntityRepository;

	protected void init() {
		salePriceDao = SpringHelper.getBean(SalePriceDao.class);
		setDao(salePriceDao);
	}

	public void saveGridData(List<SalePricePo> salePricePos) {
		for (SalePricePo salePricePo : salePricePos) {
			if(StringUtils.isEmpty(salePricePo.getId())){
				salePricePo.setId(getIdGenerator().genSid());
				salePriceDao.create(salePricePo);
			}else{
				salePriceDao.update(salePricePo);
			}
		}
	}

	public void updatePrice(Map<String, Object> dataMap, String skuId) {

		// 删除旧价格
		if (StringUtils.isNotBlank(skuId)) {
			List<SalePricePo> salePricePos = salePriceRepository.findBySkuId(skuId);
			for (SalePricePo salePricePo : salePricePos) {
				destroy(salePricePo.getId());
			}
		}

		// 保存价
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("whereSql", "id_ in( select attr_id_ from skg_eav_set_attr  where group_id_ in (  select id_ from skg_eav_set_group "
				+ "where set_id_ =  (select id_ from  skg_eav_set where key_='price')))");
		List<EavAttributePo> eavAttributePos = eavAttributeRepository.findAll(paramMap);// 获取价格类型列表

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<SalePricePo> salePricePos = new ArrayList<SalePricePo>();
		for (EavAttributePo eavAttributePo : eavAttributePos) {
			for (Entry<String, Object> entry : dataMap.entrySet()) {
				SalePricePo salePricePo = new SalePricePo();
				String key = entry.getKey().trim();
				String value = entry.getValue().toString().trim();
				if (eavAttributePo.getKey().equals(key)) {

					salePricePo.setType(key);
					salePricePo.setPrice(Double.valueOf(value));
					salePricePo.setMinNum(1);
					salePricePo.setMaxTotal(Integer.valueOf((String) dataMap.get(key + "MaxNum")));
					salePricePo.setProdSkuId(skuId);
					salePricePo.setSaleTotal(0);
					try {
						if (StringUtils.isNotBlank(dataMap.get(key + "OnSaleBegin").toString())) {
							salePricePo.setFromTime(sdf.parse(dataMap.get(key + "OnSaleBegin").toString()));
						}
						if (StringUtils.isNotBlank(dataMap.get(key + "OnSaleEnd").toString())) {
							salePricePo.setToTime(sdf.parse(dataMap.get(key + "OnSaleEnd").toString()));
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
					salePricePo.setStatus("on_sale");
					SalePrice salePrice = new SalePrice();
					salePricePos.add(salePricePo);
					salePrice.setData(salePricePo);
					salePrice.save();

					// 更新solr
					SaleSku saleSku = saleSkuRepository.load(skuId);

					SaleSkuPo saleSkuPo = saleSku.getData();
					List<String> ids = new ArrayList<String>();
					ids.add(saleSkuPo.getSaleProdId());
					Map<String, Object> params = new HashMap<String, Object>();
					params.put("whereSql", "s.sale_id_=" + saleSkuPo.getSaleProdId());
					List<SaleEntityPo> saleEntityPos = saleEntityRepository.find2(params);
					for (SaleEntityPo saleEntityPo : saleEntityPos) {
						if (!"package".equals(saleEntityPo.getSeType()) && !"rush".equals(saleEntityPo.getSalesType()) && "Y".equals(saleEntityPo.getIsSearch())) {
							saleSkuService.getSaleSkuForSolr(saleSkuPo);
						//	SolrSaleUtils.newInstance().add(saleSkuPo);
							// SkuSolr.add(saleSkuPo);
						}
					}

				}
			}
		}
	}

}
